package cn.org.donald.service.impl;

import cn.org.donald.framework.util.BeanUtil;
import cn.org.donald.framework.util.ListUtil;
import cn.org.donald.mapper.FeatureMapper;
import cn.org.donald.mapper.GameMapper;
import cn.org.donald.mapper.TypeMapper;
import cn.org.donald.pojo.dto.game.GameFeatureDto;
import cn.org.donald.pojo.entity.game.Feature;
import cn.org.donald.pojo.entity.game.Game;
import cn.org.donald.pojo.entity.game.GameFeature;
import cn.org.donald.mapper.GameFeatureMapper;
import cn.org.donald.pojo.entity.game.Type;
import cn.org.donald.pojo.vo.GameVo;
import cn.org.donald.service.game.GameFeatureService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Donald
 * @since 2020-12-12
 */
@Service(interfaceClass = GameFeatureService.class, version = "1.0.0", group = "donald")
public class GameFeatureServiceImpl extends ServiceImpl<GameFeatureMapper, GameFeature> implements GameFeatureService {

    @Autowired
    private GameMapper gameMapper;

    @Autowired
    private GameFeatureMapper gameFeatureMapper;

    @Autowired
    private FeatureMapper featureMapper;

    @Autowired
    private TypeMapper typeMapper;

    /**
     * 新增游戏和特性关联记录
     *
     * @param gameFeatureDto
     * @return
     */
    @Override
    public boolean add(GameFeatureDto gameFeatureDto) {
        GameFeature gameFeature = new GameFeature();
        BeanUtil.copyProperties(gameFeatureDto,gameFeature);
        return super.save(gameFeature);
    }

    /**
     * 删除游戏和特性关联记录
     *
     * @param gameFeatureDto
     * @return
     */
    @Override
    public boolean delete(GameFeatureDto gameFeatureDto) {
        QueryWrapper<GameFeature> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("game_id",gameFeatureDto.getGameId());
        queryWrapper.eq("feature_id",gameFeatureDto.getFeatureId());
        return super.remove(queryWrapper);
    }

    /**
     * 根据特性查询
     *
     * @param featureId
     * @return
     */
    @Override
    public List<GameVo> findGameByFeatureId(Long featureId) {
        QueryWrapper<GameFeature> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("feature_id", featureId);
        List<GameFeature> gameFeatureList = super.list(queryWrapper);
        List<Long> gameIds = new ArrayList<>();
        gameFeatureList.forEach(gameFeature -> {
            if (gameFeature.getGameId()!=null){
                gameIds.add(gameFeature.getGameId());
            }
        });
        if (gameIds.isEmpty()){
            return new ArrayList<>();
        }
        List<Game> gameList = gameMapper.selectBatchIds(gameIds);
        List<GameVo> gameVos = new ArrayList<>(gameList.size());
        gameList.forEach(game -> {
            GameVo gameVo = new GameVo();
            List<Feature> features = featureMapper.selectByGameId(game.getId());
            BeanUtil.copyProperties(game,gameVo);
            gameVo.setFeatureList(features);
            Type type = typeMapper.selectById(game.getTypeId());
            gameVo.setType(type);
            gameVos.add(gameVo);

        });
        return gameVos;
    }

    /**
     * 根据游戏id查询特性
     *
     * @param gameId
     * @return
     */
    @Override
    public List<Feature> findFeatureByGameId(Long gameId) {
        QueryWrapper<GameFeature> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("game_id",gameId);
        List<GameFeature> gameFeatureList = super.list(queryWrapper);
        List<Long> featureIds = new ArrayList<>();
        gameFeatureList.forEach(gameFeature -> {
            if(gameFeature.getGameId()!=null){
                featureIds.add(gameFeature.getFeatureId());
            }
        });
        if (featureIds.isEmpty()){
            return new ArrayList<>();
        }
        return featureMapper.selectBatchIds(featureIds);
    }
}
